<?php
// +----------------------------------------------------------------------
// | 杭州十禾信息科技有限公司
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2015 http://www.shelpe.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: LeePuman <ysyzlsm@163.com>
// +----------------------------------------------------------------------

/**
 * 计划事件
 */
Class CamapaignEvent extends NewzanAction
{
    public function setAutoDriveTitle($params)
    {
        $campaign_id = $params['campaign_id'];
        try {
            if(!$campaign_id)
                throw new Exception("计划id错误", 1);

            $campaignMdl = D('Campaign');

            $res = $campaignMdl->getCountLikeTitle($params['title'], $params['user_id']);
            if($res) {
                $campaign = array_column($res, 'title');
                $num = array();
                foreach ($campaign as $title) {
                    preg_match("/{$params['title']}(\d*)/", $title, $matches);
                    $num[] = $matches[1] ? (int)$matches[1] : 0;
                }
                $count = max($num) + 1;
            } else {
                $count = 1;
            }
            
            $status = array(
                'campaign_id' => $campaign_id,
                'title' => $params['title'] . $count,
                'online_status' => $params['online_status']
            );
            
            $campaignMdl->updateCampaignStatus($params['user_id'], $campaign_id, $status);
        } catch (Exception $e) {
            exit($e->getMessage());
        }
    }
}
